package com.taomeng.service.impl;


import com.taomeng.dao.UserDao;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.Role;
import com.taomeng.entity.User;
import com.taomeng.entity.UserInfo;
import com.taomeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    public PageBean<UserInfo> findByPage(int page, int limit) {
        return userDao.findByPage(page, limit);
    }

    @Override
    public List<User> getUsersByDeptId(int deptId) {
        return userDao.getUsersByDeptId(deptId);
    }

    @Override
    public boolean add(User user, int roleId) {
        User byUsername = userDao.findByUsername(user.getUsername());
        if (null != byUsername) {
            return false;
        }
        Long generatedId  = userDao.add(user);

        return  userDao.addRoleByUserId(generatedId, roleId);

    }

    @Override
    public boolean delById(Long userId) {
        if (userDao.delRoleByUserId(userId)) {
            return userDao.delById(userId);
        }
        return false;
    }

    @Override
    public boolean update(User user, int roleId) {
        if (userDao.updateRoleByUserId(user.getId(),roleId)) {
            return userDao.updateUser(user);
        }

        return false;
    }
}
